package com.xc.hotelmgr.order.modules.order.service.impl;

import com.google.common.collect.Lists;
import com.xc.hotelmgr.order.common.utils.*;
import com.xc.hotelmgr.order.modules.ali.to.common.DailyInfo;
import com.xc.hotelmgr.order.modules.order.service.DailyroomService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.xc.hotelmgr.order.modules.order.dao.DailyroomDao;
import com.xc.hotelmgr.order.modules.order.entity.DailyroomEntity;

@Slf4j
@Service("dailyroomService")
public class DailyroomServiceImpl extends ServiceImpl<DailyroomDao, DailyroomEntity> implements DailyroomService {
    @Autowired
    private RedisUtils redisUtils;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<DailyroomEntity> page = this.page(
                new Query<DailyroomEntity>().getPage(params),
                new QueryWrapper<DailyroomEntity>()
        );

        return new PageUtils(page);
    }
    @Async
    @Override
    public void updateRs(String rpCode,List<DailyInfo> dailyInfos) {
        List<DailyroomEntity> collect = dailyInfos.stream().map(item -> {
            DailyroomEntity dailyroomEntity = new DailyroomEntity();
            String rpCodeDay = item.getDay().replace("-","");
            rpCodeDay = rpCodeDay.substring(rpCode.length()-4);
            dailyroomEntity.setId(Long.valueOf(rpCode + rpCodeDay));
            dailyroomEntity.setPriceDate(DateUtils.stringToDate(item.getDay(),"yyyy-MM-dd"));
            dailyroomEntity.setPrice(Integer.valueOf(item.getPrice()));
            return  dailyroomEntity;
        }).collect(Collectors.toList());
        this.updateBatchById(collect);
    }

//    @Override
//    public List<DailyroomEntity> sqlSelect(String ratePlanCode, String checkIn, String checkOut) {
//        return baseMapper.sqlSelect(ratePlanCode,checkIn,checkOut);
//    }

    @Override
    public List<DailyroomEntity> redisSelect(String ratePlanCode, String checkIn, String checkOut) {
        String h = null;
        List<String> fileds = Lists.newArrayList();
        fileds.add("p");
        fileds.add("n");
        Date stDate = DateUtils.stringToDate(checkIn,"yyyy-MM-dd");
        Date enDate = DateUtils.stringToDate(checkOut,"yyyy-MM-dd");
        List<DailyroomEntity> dailyroomEntities = Lists.newArrayList();
        DailyroomEntity dailyroomEntity = null;
        while (stDate.before(enDate)){
            dailyroomEntity = new DailyroomEntity();
            h = Constant.RedisKey.RPS_PREFIX.getValue() + ratePlanCode + DateUtils.getDateMMdd(stDate);
            List<Object>  dailyRooms = redisUtils.hashGetAll(h,fileds);
            if(dailyRooms == null || dailyRooms.get(0) ==null || dailyRooms.get(1) ==null){
                log.error("验证价格库存接口，定价区间内rps不存在,rp：{},日期：{},dailyRooms:{}",ratePlanCode,stDate,dailyRooms);
                stDate = DateUtils.addDateDays(stDate,1);
                continue;
            }
            dailyroomEntity.setRpCode(Long.valueOf(ratePlanCode));
            dailyroomEntity.setPriceDate(stDate);
            dailyroomEntity.setPrice(Integer.valueOf(dailyRooms.get(0).toString()));
            dailyroomEntity.setSaleNum(Integer.valueOf(dailyRooms.get(1).toString()));
            dailyroomEntities.add(dailyroomEntity);
            stDate = DateUtils.addDateDays(stDate,1);
        }
        return dailyroomEntities;
    }
}